system-controller: fixes in surface Z order handling
[profile/ivi/murphy.git] / packaging.in / murphy.lua
1 with_system_controller = false
2 with_amb = false
3 verbose = 0
4
5 m = murphy.get()
6
7 -- try loading the various logging plugins
8 m:try_load_plugin('systemd')
9 m:try_load_plugin('dlog')
10
11 -- load the console plugin
12 m:try_load_plugin('console')
13
14 m:try_load_plugin('console.disabled', 'webconsole', {
15                   address = 'wsck:127.0.0.1:3000/murphy',
16                   httpdir = '/usr/share/murphy/webconsole' });
17
18 -- load the dbus plugin
19 if m:plugin_exists('dbus') then
20     m:load_plugin('dbus')
21 end
22
23 -- load the native resource plugin
24 if m:plugin_exists('resource-native') then
25     m:load_plugin('resource-native')
26     m:info("native resource plugin loaded")
27 else
28     m:info("No native resource plugin found...")
29 end
30
31 -- load the dbus resource plugin
32 m:try_load_plugin('resource-dbus', {
33     dbus_bus = "system",
34     dbus_service = "org.Murphy",
35     dbus_track = true,
36     default_zone = "driver",
37     default_class = "implicit"
38 })
39
40 -- load the domain control plugin
41 if m:plugin_exists('domain-control') then
42     m:load_plugin('domain-control')
43 else
44     m:info("No domain-control plugin found...")
45 end
46
47 if m:plugin_exists('glib') then
48     m:load_plugin('glib')
49 else
50     m:info("No glib plugin found...")
51 end
52
53 -- load the AMB plugin
54 if m:plugin_exists('amb') then
55     m:try_load_plugin('amb')
56
57     if builtin.method.amb_initiate and
58        builtin.method.amb_update
59     then
60         with_amb = true
61     end
62 else
63     m:info("No amb plugin found...")
64 end
65
66 -- load the ASM resource plugin
67 if m:plugin_exists('resource-asm') then
68     m:try_load_plugin('resource-asm', {
69         zone = "driver",
70         share_mmplayer = "player:AVP,mandatory,exclusive,strict",
71         ignored_argv0 = "WebProcess"
72     })
73 else
74     m:info("No audio session manager plugin found...")
75 end
76
77 if m:plugin_exists('system-controller') then
78     with_system_controller = true
79 elseif m:plugin_exists('ivi-resource-manager') then
80     m:load_plugin('ivi-resource-manager')
81     with_system_controller = false
82 end
83
84 -- define application classes
85 application_class {
86     name     = "interrupt",
87     priority = 99,
88     modal    = true ,
89     share    = false,
90     order    = "fifo"
91 }
92
93 application_class {
94     name     = "emergency",
95     priority = 80,
96     modal    = false,
97     share    = false,
98     order    = "fifo"
99 }
100 application_class {
101     name     = "system",
102     priority = 52,
103     modal    = false,
104     share    = true,
105     order    = "lifo"
106 }
107 application_class {
108     name     = "alert",
109     priority = 51,
110     modal    = false,
111     share    = false,
112     order    = "fifo"
113 }
114
115 application_class {
116     name     = "navigator",
117     priority = 50,
118     modal    = false,
119     share    = true,
120     order    = "fifo"
121 }
122
123 application_class {
124     name     = "phone",
125     priority = 6 ,
126     modal    = false,
127     share    = true ,
128     order    = "lifo"
129 }
130 application_class {
131     name     = "camera",
132     priority = 5,
133     modal    = false,
134     share    = false,
135     order    = "lifo"
136 }
137
138 application_class { name="event"    , priority=4 , modal=false, share=true , order="fifo" }
139 application_class { name="game"     , priority=3 , modal=false, share=false, order="lifo" }
140 --# doesn't need to be created here, ivi-resource-manager creates it if loaded
141 --#application_class { name="basic"    , priority=2 , modal=false, share=false, order="lifo" }
142 application_class { name="player"   , priority=1 , modal=false, share=true , order="lifo" }
143 application_class { name="implicit" , priority=0 , modal=false, share=false, order="lifo" }
144
145 -- define zone attributes
146 zone.attributes {
147     type = {mdb.string, "common", "rw"},
148     location = {mdb.string, "anywhere", "rw"}
149 }
150
151 -- define zones
152 zone {
153      name = "driver",
154      attributes = {
155          type = "common",
156          location = "front-left"
157      }
158 }
159
160 zone {
161      name = "passanger1",
162      attributes = {
163          type = "private",
164          location = "front-right"
165      }
166 }
167
168 zone {
169      name = "passanger2",
170      attributes = {
171          type = "private",
172          location = "back-left"
173      }
174 }
175
176 zone {
177      name = "passanger3",
178      attributes = {
179          type = "private",
180          location = "back-right"
181      }
182 }
183
184 zone {
185      name = "passanger4",
186      attributes = {
187          type = "private",
188          location = "back-left"
189      }
190 }
191
192
193 -- define resource classes
194 if not m:plugin_exists('ivi-resource-manager') and
195    not with_system_controller
196 then
197    resource.class {
198         name = "audio_playback",
199         shareable = true,
200         attributes = {
201             role = { mdb.string, "music", "rw" },
202             pid = { mdb.string, "<unknown>", "rw" },
203             policy = { mdb.string, "relaxed", "rw" }
204         }
205    }
206 end
207
208 resource.class {
209      name = "audio_recording",
210      shareable = true,
211      attributes = {
212          role   = { mdb.string, "music"    , "rw" },
213          pid    = { mdb.string, "<unknown>", "rw" },
214          policy = { mdb.string, "relaxed"  , "rw" }
215      }
216 }
217
218 resource.class {
219      name = "video_playback",
220      shareable = false,
221 }
222
223 resource.class {
224      name = "video_recording",
225      shareable = false
226 }
227
228 resource.class {
229      name = "speech_recognition",
230      shareable = true
231 }
232
233 resource.class {
234      name = "speech_synthesis",
235      shareable = true
236 }
237
238 -- PulseAudio volume context
239 mdb.table {
240     name = "volume_context",
241     index = { "id" },
242     create = true,
243     columns = {
244         { "id", mdb.unsigned },
245         { "value", mdb.string, 64 },
246     }
247 }
248
249 -- put default volume context to the table
250 mdb.table.volume_context:insert({ id = 1, value = "default" })
251
252 if not m:plugin_exists('ivi-resource-manager') and
253    not with_system_controller
254 then
255     resource.method.veto = {
256         function(zone, rset, grant, owners, req_set)
257             return true
258          end
259     }
260 end
261
262 -- test for creating selections
263 mdb.select {
264            name = "audio_owner",
265            table = "audio_playback_owner",
266            columns = {"application_class"},
267            condition = "zone_name = 'driver'"
268 }
269
270 mdb.select {
271            name = "vehicle_speed",
272            table = "amb_vehicle_speed",
273            columns = {"value"},
274            condition = "key = 'VehicleSpeed'"
275 }
276
277 element.lua {
278    name    = "speed2volume",
279    inputs  = { speed = mdb.select.vehicle_speed, param = 9 },
280    outputs = {  mdb.table { name = "speedvol",
281                             index = {"zone", "device"},
282                             columns = {{"zone", mdb.string, 16},
283                                        {"device", mdb.string, 16},
284                                        {"value", mdb.floating}},
285                             create = true
286                            }
287              },
288    oldvolume = 0.0,
289    update  = function(self)
290                 speed = self.inputs.speed.single_value
291                 if (speed) then
292                     volume = (speed - 144.0) / 7.0
293                 else
294                     volume = 0.0
295                 end
296                 diff = volume - self.oldvolume
297                 if (diff*diff > self.inputs.param) then
298                     print("*** element "..self.name.." update "..volume)
299                     self.oldvolume = volume
300                     mdb.table.speedvol:replace({zone = "driver", device = "speakers", value = volume})
301                 end
302              end
303 }
304
305 mdb.select {
306     name = "amb_state",
307     table = "amb_state",
308     columns = { "state" },
309     condition = "id = 0"
310 }
311
312 -- Night mode processing chain
313
314 mdb.select {
315     name = "exterior_brightness",
316     table = "amb_exterior_brightness",
317     columns = { "value" },
318     condition = "key = 'ExteriorBrightness'"
319 }
320
321 element.lua {
322     name    = "nightmode",
323     inputs  = { brightness = mdb.select.exterior_brightness },
324     oldmode = -1;
325     outputs = {
326         mdb.table {
327             name = "amb_nightmode",
328             index = { "id" },
329             create = true,
330             columns = {
331                 { "id", mdb.unsigned },
332                 { "night_mode", mdb.unsigned }
333             }
334         }
335     },
336     update = function(self)
337         -- This is a trivial function to calculate night mode. Later, we will
338         -- need a better threshold value and hysteresis to prevent oscillation.
339
340         brightness = self.inputs.brightness.single_value
341
342         if not brightness then
343             return
344         end
345
346         print("*** element "..self.name.." update brightness: "..brightness)
347
348         if brightness > 300 then
349             mode = 0
350         else
351             mode = 1
352         end
353
354         print("*** resulting mode: ".. mode)
355
356         if not (mode == self.oldmode) then
357             mdb.table.amb_nightmode:replace({ id = 0, night_mode = mode })
358         end
359
360         self.oldmode = mode
361     end
362 }
363
364 mdb.select {
365     name = "select_night_mode",
366     table = "amb_nightmode",
367     columns = { "night_mode" },
368     condition = "id = 0"
369 }
370
371 if with_amb then
372     sink.lua {
373         name = "night_mode",
374         inputs = { NightMode = mdb.select.select_night_mode,
375                    amb_state = mdb.select.amb_state },
376         property = "NightMode",
377         type = "b",
378         initiate = builtin.method.amb_initiate,
379         update = builtin.method.amb_update
380     }
381 end
382
383 -- Night mode general handlers
384
385 if with_system_controller then
386     sink.lua {
387         name = "nightmode_homescreen",
388         inputs = { owner = mdb.select.select_night_mode },
389         initiate = function(self)
390                 -- data = mdb.select.select_night_mode.single_value
391                 return true
392             end,
393         update = function(self)
394                 send_night_mode_to_home_screen()
395             end
396     }
397 end
398
399 -- Driving mode processing chain
400
401 element.lua {
402     name    = "drivingmode",
403     inputs  = { speed = mdb.select.vehicle_speed },
404     oldmode = -1;
405     outputs = {
406         mdb.table {
407             name = "amb_drivingmode",
408             index = { "id" },
409             create = true,
410             columns = {
411                 { "id", mdb.unsigned },
412                 { "driving_mode", mdb.unsigned }
413             }
414         }
415     },
416     update = function(self)
417
418         speed = self.inputs.speed.single_value
419
420         if not speed then
421             return
422         end
423
424         if speed == 0 then
425             mode = 0
426         else
427             mode = 1
428         end
429
430         if not (mode == self.oldmode) then
431             mdb.table.amb_drivingmode:replace({ id = 0, driving_mode = mode })
432         end
433
434         self.oldmode = mode
435     end
436 }
437
438 mdb.select {
439     name = "select_driving_mode",
440     table = "amb_drivingmode",
441     columns = { "driving_mode" },
442     condition = "id = 0"
443 }
444
445 if with_amb then
446     sink.lua {
447         name = "driving_mode",
448         inputs = { DrivingMode = mdb.select.select_driving_mode,
449                    amb_state = mdb.select.amb_state },
450         property = "DrivingMode",
451         type = "u",
452         initiate = builtin.method.amb_initiate,
453         update = builtin.method.amb_update
454     }
455 end
456
457 -- turn signals (left, right)
458
459 mdb.select {
460     name = "winker",
461     table = "amb_turn_signal",
462     columns = { "value" },
463     condition = "key = 'TurnSignal'"
464 }
465
466 -- define three categories
467
468 mdb.select {
469     name = "undefined_applications",
470     table = "aul_applications",
471     columns = { "appid" },
472     condition = "category = '<undefined>'"
473 }
474
475 mdb.select {
476     name = "basic_applications",
477     table = "aul_applications",
478     columns = { "appid" },
479     condition = "category = 'basic'"
480 }
481
482 mdb.select {
483     name = "entertainment_applications",
484     table = "aul_applications",
485     columns = { "appid" },
486     condition = "category = 'entertainment'"
487 }
488
489 function ft(t)
490     -- filter the object garbage out of the tables
491     ret = {}
492
493     for k,v in pairs(t) do
494         if k ~= "userdata" and k ~= "new" then
495             ret[k] = v
496         end
497     end
498
499     return ret
500 end
501
502 function getApplication(appid)
503     local conf = nil
504
505     -- find the correct local application definition
506
507     for k,v in pairs(ft(application)) do
508         if appid == v.appid then
509             conf = v
510             break
511         end
512     end
513
514     return conf
515 end
516
517 function regulateApplications(t, regulation)
518     for k,v in pairs(ft(t)) do
519
520         -- iterate through the undefined and entertainment apps, see if
521         -- they have been overruled in local config
522
523         local conf = getApplication(v.appid)
524
525         if conf and conf.resource_class ~= "player" then
526             -- override, don't disable
527             resmgr:disable_screen_by_appid("*", "*", v.appid, false, false)
528         else
529             resmgr:disable_screen_by_appid("*", "*", v.appid, regulation == 1, false)
530         end
531     end
532     resource.method.recalc("driver")
533 end
534
535 -- regulation (on), use "select_driving_mode"
536
537 sink.lua {
538     name = "driving_regulation",
539     inputs = { owner = mdb.select.select_driving_mode },
540     initiate = function(self)
541         -- local data = mdb.select.select_driving_mode.single_value
542         return true
543     end,
544     update = function(self)
545         local data = mdb.select.select_driving_mode.single_value
546
547         if verbose > 1 then
548             print("Driving mode updated: " .. tostring(data))
549         end
550
551         if not sc then
552             return true
553         end
554
555         -- tell homescreen that driving mode was updated
556         send_driving_mode_to_home_screen()
557
558         regulateApplications(ft(mdb.select.entertainment_applications), data)
559         regulateApplications(ft(mdb.select.undefined_applications), data)
560
561         return true
562     end
563 }
564 --[[
565 sink.lua {
566     name = "regulated_app_change",
567     inputs = { undef = mdb.select.undefined_applications,
568                entertainment = mdb.select.entertainment_applications },
569     initiate = function(self)
570         return true
571     end,
572     update = function(self)
573         local data = mdb.select.select_driving_mode.single_value
574
575         if not sc then
576             return
577         end
578
579         if verbose > 1 then
580             print("regulated application list was changed")
581         end
582
583         regulateApplications(ft(mdb.select.entertainment_applications), data)
584         regulateApplications(ft(mdb.select.undefined_applications), data)
585
586         return true
587     end
588 }
589 --]]
590
591 -- shift position (parking, reverse, other)
592
593 mdb.select {
594     name = "gear_position",
595     table = "amb_gear_position",
596     columns = { "value" },
597     condition = "key = 'GearPosition'"
598 }
599
600 -- cameras (back, front, left, right)
601
602 element.lua {
603     name    = "camera_state",
604     inputs  = { winker = mdb.select.winker, gear = mdb.select.gear_position },
605     oldmode = -1;
606     outputs = {
607         mdb.table {
608             name = "target_camera_state",
609             index = { "id" },
610             create = true,
611             columns = {
612                 { "id", mdb.unsigned },
613                 { "front_camera", mdb.unsigned },
614                 { "back_camera", mdb.unsigned },
615                 { "right_camera", mdb.unsigned },
616                 { "left_camera", mdb.unsigned }
617             }
618         }
619     },
620     update = function(self)
621
622         front_camera = 0
623         back_camera = 0
624         right_camera = 0
625         left_camera = 0
626
627         if self.inputs.gear == 128 then
628             back_camera = 1
629         elseif self.inputs.winker == 1 then
630             right_camera = 1
631         elseif self.inputs.winker == 2 then
632             left_camera = 1
633         end
634
635         mdb.table.target_camera_state:replace({ id = 0, front_camera = front_camera, back_camera = back_camera, right_camera = right_camera, left_camera = left_camera })
636
637     end
638 }
639
640 -- system controller test setup
641
642 if not with_system_controller then
643    -- ok, we should have 'audio_playback' defined by now
644    m:try_load_plugin('telephony')
645    return
646 end
647
648 m:load_plugin('system-controller')
649
650 window_manager_operation_names = {
651     [1] = "create",
652     [2] = "destroy"
653 }
654
655 function window_manager_operation_name(oper)
656     local name = window_manager_operation_names[oper]
657     if name then return name end
658     return "<unknown " .. tostring(oper) .. ">"
659 end
660
661 window_operation_names = {
662     [1] = "create",
663     [2] = "destroy",
664     [3] = "name_change",
665     [4] = "visible",
666     [5] = "configure",
667     [6] = "active",
668     [7] = "map",
669     [8] = "hint"
670 }
671
672 function window_operation_name(oper)
673     local name = window_operation_names[oper]
674     if name then return name end
675     return "<unknown " .. tostring(oper) .. ">"
676 end
677
678 layer_operation_names = {
679     [1] = "create",
680     [2] = "destroy",
681     [3] = "visible"
682 }
683
684 function layer_operation_name(oper)
685     local name = layer_operation_names[oper]
686     if name then return name end
687     return "<unknown " .. tostring(oper) .. ">"
688 end
689
690 input_manager_operation_names = {
691     [1] = "create",
692     [2] = "destroy",
693     [3] = "ready"
694 }
695
696 function input_manager_operation_name(oper)
697     local name = input_manager_operation_names[oper]
698     if name then return name end
699     return "<unknown " .. tostring(oper) .. ">"
700 end
701
702 input_operation_names = {
703     [1] = "create",
704     [2] = "destroy",
705     [3] = "update"
706 }
707
708 function input_operation_name(oper)
709     local name = input_operation_names[oper]
710     if name then return name end
711     return "<unknown " .. tostring(oper) .. ">"
712 end
713
714 code_operation_names = {
715     [1] = "create",
716     [2] = "destroy",
717     [3] = "state_change"
718 }
719
720 function code_operation_name(oper)
721     local name = code_operation_names[oper]
722     if name then return name end
723     return "<unknown " .. tostring(oper) .. ">"
724 end
725
726 command_names = {
727     [0x00001] = "send_appid",
728     [0x10001] = "create",
729     [0x10002] = "destroy",
730     [0x10003] = "show",
731     [0x10004] = "hide",
732     [0x10005] = "move",
733     [0x10006] = "change_active",
734     [0x10007] = "change_layer",
735     [0x10008] = "change_attr",
736     [0x10009] = "name",
737     [0x10011] = "map_thumb",
738     [0x10012] = "unmap_thumb",
739     [0x10020] = "show layer",
740     [0x10021] = "hide_layer",
741     [0x10022] = "change_layer_attr",
742     [0x20001] = "add_input",
743     [0x20002] = "del_input",
744     [0x20003] = "send_input",
745     [0x40001] = "acquire_res",
746     [0x40002] = "release_res",
747     [0x40003] = "deprive_res",
748     [0x40004] = "waiting_res",
749     [0x40005] = "revert_res",
750     [0x40011] = "create_res",
751     [0x40012] = "destroy_res",
752     [0x50001] = "set_region",
753     [0x50002] = "unset_region",
754     [0x60001] = "change_state"
755 }
756
757 function command_name(command)
758     local name = command_names[command]
759     if name then return name end
760     return "<unknown " .. tostring(command) .. ">"
761 end
762
763 input_layer = {
764    [3] = true, -- input
765    [4] = true, -- touch
766    [5] = true  -- cursor
767 }
768
769 -- some day this should be merged with wmgr.layers
770 ico_layer_type = {
771    [1] = 0x1000, -- background
772    [2] = 0x2000, -- application
773    [3] = 0x4000, -- input
774    [4] = 0xa000, -- touch
775    [5] = 0xb000, -- cursor
776    [6] = 0xc000, -- startup
777    [7] = 0x3000  -- fullscreen
778 }
779
780 resmgr = resource_manager {
781   screen_event_handler = function(self, ev)
782                              local event = ev.event
783                              local surface = ev.surface
784
785                              if event == "init" then
786                                  if verbose > 0 then
787                                      print("*** init screen resource allocation -- disable all 'player'")
788                                  end
789                                  resmgr:disable_audio_by_appid("*", "player", "*", true, false)
790                              elseif event == "preallocate" then
791                                  if verbose > 0 then
792                                      print("*** preallocate screen resource "..
793                                            "for '" .. ev.appid .. "' -- enable 'player', if any")
794                                  end
795                                  resmgr:disable_audio_by_appid("*", "player", ev.appid, false, false)
796                              elseif event == "grant" then
797                                  if verbose > 0 then
798                                     print("*** make visible surface "..surface)
799                                  end
800                                  local a = animation({})
801                                  local r = m:JSON({surface = surface,
802                                                    visible = 1,
803                                                    raise   = 1})
804                                  wmgr:window_request(r,a,0)
805                              elseif event == "revoke" then
806                                  if verbose > 0 then
807                                     print("*** hide surface "..surface)
808                                  end
809                                  local a = animation({})
810                                  local r = m:JSON({surface = ev.surface,
811                                                    visible = 0})
812                                  wmgr:window_request(r,a,0)
813
814                              elseif event == "create" then
815
816                                 if verbose > 0 then
817                                     print("*** screen resource event: " ..
818                                           tostring(ev))
819                                 end
820
821                                 local regulation = mdb.select.select_driving_mode.single_value
822
823                                 if regulation == 1 then
824
825                                     local blacklisted = false
826
827                                     -- applications which have their category set to "entertainment"
828                                     -- or "undefined" are blacklisted, meaning they should be regulated
829
830                                     for i,v in pairs(ft(mdb.select.undefined_applications)) do
831                                         if v.appid == ev.appid then
832                                             if verbose > 0 then
833                                                 print(ev.appid .. " was blacklisted (undefined)")
834                                             end
835                                             blacklisted = true
836                                             break
837                                         end
838                                     end
839
840                                     if not blacklisted then
841                                         for i,v in pairs(ft(mdb.select.entertainment_applications)) do
842                                             if v.appid == ev.appid then
843                                                 if verbose > 0 then
844                                                     print(ev.appid .. " was blacklisted (entertainment)")
845                                                 end
846                                                 blacklisted = true
847                                                 break
848                                             end
849                                         end
850                                     end
851
852                                     -- our local application config, which takes precedence
853                                     local conf = getApplication(ev.appid)
854
855                                     -- disable only non-whitelisted applications
856                                     if not conf or conf.resource_class == "player" then
857                                         if blacklisted then
858                                             if verbose > 0 then
859                                                 print("disabling screen for " .. ev.appid)
860                                             end
861                                             resmgr:disable_screen_by_appid("*", "*", ev.appid, true, true)
862                                         end
863                                     end
864                                 end
865
866                              elseif event == "destroy" then
867                                if verbose > 0 then
868                                     print("*** screen resource event: " ..
869                                           tostring(ev))
870                                  end
871                              else
872                                  if verbose > 0 then
873                                     print("*** screen resource event: " ..
874                                           tostring(ev))
875                                  end
876                              end
877                          end,
878   audio_event_handler = function(self, ev)
879                              local event = ev.event
880                              local appid = ev.appid
881                              local audioid = ev.audioid
882
883                              if event == "grant" then
884                                  if verbose > 0 then
885                                     print("*** grant audio to "..appid..
886                                           " ("..audioid..") in '" ..
887                                           ev.zone .. "' zone")
888                                  end
889                              elseif event == "revoke" then
890                                  if verbose > 0 then
891                                     print("*** revoke audio from "..appid..
892                                           " ("..audioid..") in '" ..
893                                           ev.zone .. "' zone")
894                                  end
895                              else
896                                  if verbose > 0 then
897                                     print("*** audio resource event: " ..
898                                           tostring(ev))
899                                  end
900                              end
901                         end
902 }
903
904 resclnt = resource_client {}
905
906 wmgr = window_manager {
907   geometry = function(self, w,h, v)
908                   if type(v) == "function" then
909                       return v(w,h)
910                   end
911                   return v
912              end,
913
914   application = function(self, appid)
915                      if appid then
916                          local app = application_lookup(appid)
917                          if not app then
918                              app = application_lookup("default")
919                          end
920                          return app
921                      end
922                      return { privileges = {screen="none", audio="none"} }
923                 end,
924
925   output_order = { 1, 0 },
926
927   outputs = { { name  = "Mid",
928                 id    = 1,
929                 zone  = "driver",
930                 areas = { Full = {
931                               id     = 20,
932                               pos_x  = 0,
933                               pos_y  = 0,
934                               width  = function(w,h) return w end,
935                               height = function(w,h) return h end
936                           },
937                           Left = {
938                               id     = 21,
939                               pos_x  = 0,
940                               pos_y  = 0,
941                               width  = 320,
942                               height = function(w,h) return h end
943                           },
944                           Right = {
945                               id     = 22,
946                               pos_x  = function(w,h) return w-320 end,
947                               pos_y  = 0,
948                               width  = 320,
949                               height = function(w,h) return h end
950                           }
951                         }
952                },
953                { name  = "Center",
954                  id    = 4,
955                  zone  = "driver",
956                  areas = { Status = {
957                               id     = 0,
958                               pos_x  = 0,
959                               pos_y  = 0,
960                               width  = function(w,h) return w end,
961                               height = 64
962                            },
963                            Full = {
964                               id     = 1,
965                               pos_x  = 0,
966                               pos_y  = 64,
967                               width  = function(w,h) return w end,
968                               height = function(w,h) return h-64-128 end
969                            },
970                            Upper = {
971                               id     = 2,
972                               pos_x  = 0,
973                               pos_y  = 64,
974                               width  = function(w,h) return w end,
975                               height = function(w,h) return (h-64-128)/2 end
976                            },
977                            Lower = {
978                               id     = 3,
979                               pos_x  = 0,
980                               pos_y  = function(w,h) return (h-64-128)/2+64 end,
981                               width  = function(w,h) return w end,
982                               height = function(w,h) return (h-64-128)/2 end
983                            },
984                            UpperLeft = {
985                               id     = 4,
986                               pos_x  = 0,
987                               pos_y  = 64,
988                               width  = function(w,h) return w/2 end,
989                               height = function(w,h) return (h-64-128)/2 end
990                            },
991                            UpperRight = {
992                               id     = 5,
993                               pos_x  = function(w,h) return w/2 end,
994                               pos_y  = 64,
995                               width  = function(w,h) return w/2 end,
996                               height = function(w,h) return (h-64-128)/2 end
997                            },
998                            LowerLeft = {
999                               id     = 6,
1000                               pos_x  = 0,
1001                               pos_y  = function(w,h) return (h-64-128/2)+64 end,
1002                               width  = function(w,h) return w/2 end,
1003                               height = function(w,h) return (h-64-128)/2 end
1004                            },
1005                            LowerRight = {
1006                               id     = 7,
1007                               pos_x  = function(w,h) return w/2 end,
1008                               pos_y  = function(w,h) return (h-64-128/2)+64 end,
1009                               width  = function(w,h) return w/2 end,
1010                               height = function(w,h) return (h-64-128)/2 end
1011                            },
1012                            SysApp = {
1013                               id     = 8,
1014                               pos_x  = 0,
1015                               pos_y  = 64,
1016                               width  = function(w,h) return w end,
1017                               height = function(w,h) return h-64-128 end
1018                            },
1019                            ["SysApp.Left"] = {
1020                               id     = 9,
1021                               pos_x  = 0,
1022                               pos_y  = 64,
1023                               width  = function(w,h) return w/2-181 end,
1024                               height = function(w,h) return h-64-128 end
1025                            },
1026                            ["SysApp.Right"] = {
1027                               id     = 10,
1028                               pos_x  = function(w,h) return w/2+181 end,
1029                               pos_y  = 64,
1030                               width  = function(w,h) return w/2-181 end,
1031                               height = function(w,h) return h-64-128 end
1032                            },
1033                            Control = {
1034                               id     = 11,
1035                               pos_x  = 0,
1036                               pos_y  = function(w,h) return h-128 end,
1037                               width  = function(w,h) return w end,
1038                               height = 128
1039                            },
1040                         }
1041               }
1042   },
1043              --    id   name            type  output
1044   layers = { {      0, "Background"   ,    1, "Center" },
1045              {      1, "Application"  ,    2, "Center" },
1046              {      2, "Softkeyboard" ,    4, "Center" },
1047              {      3, "HomeScreen"   ,    2, "Center" },
1048              {      4, "ControlBar"   ,    2, "Center" },
1049              {      5, "InterruptApp" ,    2, "Center" },
1050              {      6, "OnScreen"     ,    2, "Center" },
1051              {    101, "Input"        ,    3, "Center" },
1052              {    102, "Cursor"       ,    5, "Center" },
1053              {    103, "Startup"      ,    6, "Center" },
1054              { 0x1000, "Background"   ,    1, "Center" },
1055              { 0x2000, "Normal"       ,    2, "Center" },
1056              { 0x3000, "Fullscreen"   ,    7, "Center" },
1057              { 0x4000, "InputPanel"   ,    3, "Center" },
1058              { 0xA000, "Touch"        ,    4, "Center" },
1059              { 0xB000, "Cursor"       ,    5, "Center" },
1060              { 0xC000, "Startup"      ,    6, "Center" }
1061   },
1062
1063
1064   manager_update = function(self, oper)
1065                        if verbose > 0 then
1066                            print("### <== WINDOW MANAGER UPDATE:" ..
1067                                  window_manager_operation_name(oper))
1068                        end
1069                        if oper == 1 then
1070                            local wumask = window_mask { -- raise   = true,
1071                                                         visible = true,
1072                                                         active  = true }
1073                            local wrmask = window_mask { raise   = true,
1074                                                         active  = true,
1075                                                         layer   = true }
1076                            local lumask = layer_mask  { visible = true }
1077                            local lrmask = layer_mask  { visible = true }
1078                            local req = m:JSON({
1079                                passthrough_window_update  = wumask:tointeger(),
1080                                passthrough_window_request = wrmask:tointeger(),
1081                                passthrough_layer_update   = lumask:tointeger(),
1082                                passthrough_layer_request  = lrmask:tointeger()
1083                            })
1084                            self:manager_request(req)
1085                        end
1086                    end,
1087
1088   window_update = function(self, oper, win, mask)
1089                       if verbose > 0 then
1090                           print("### <== WINDOW UPDATE oper:" ..
1091                                 window_operation_name(oper) ..
1092                                 " mask: " .. tostring(mask))
1093                           if verbose > 1 then
1094                               print(win)
1095                           end
1096                       end
1097
1098                       local arg = m:JSON({ surface = win.surface,
1099                                            winname = win.name,
1100                       })
1101                       local command = 0
1102
1103                       if oper == 1 then -- create
1104                            local layertype = win.layertype
1105                            if layertype and input_layer[layertype] then
1106                                if verbose > 0 then
1107                                    print("ignoring input panel creation")
1108                                end
1109                                return
1110                            end
1111                            command     = 0x10001
1112                       elseif oper == 2 then -- destroy
1113                            command     = 0x10002
1114                       elseif oper == 3 then  -- namechange
1115                            command     = 0x10009
1116                       elseif oper == 4 or oper == 5 then --visible or configure
1117                            local icolayer = ico_layer_type[win.layertype]
1118                            command     = 0x10008
1119                            arg.zone    = win.area
1120                            arg.node    = win.node
1121                            if icolayer then
1122                                arg.layertype = icolayer
1123                            end
1124                            arg.layer   = win.layer
1125                            arg.pos_x   = win.pos_x
1126                            arg.pos_y   = win.pos_y
1127                            arg.width   = win.width
1128                            arg.height  = win.height
1129                            arg.raise   = win.raise
1130                            arg.visible = win.visible
1131                            if win.active == 0 then
1132                                arg.active = 0
1133                            else
1134                                arg.active = 1
1135                            end
1136                       elseif oper == 6 then -- active
1137                            if win.active == 0 then
1138                                if verbose > 0 then
1139                                    print("ignoring inactive event")
1140                                end
1141                                return
1142                            end
1143                            command = 0x10006
1144                       elseif oper == 7 then -- map
1145                            local map = win.map
1146                            if not map then
1147                                return
1148                            end
1149                            if win.mapped == 0 then
1150                                command = 0x10012
1151                            else
1152                                command = 0x10011
1153                            end
1154                            arg.attr = map.type
1155                            arg.name = map.target
1156                            arg.width = map.width
1157                            arg.height = map.height
1158                            arg.stride = map.stride
1159                            arg.format = map.format
1160                       else
1161                            if verbose > 0 then
1162                                print("### nothing to do")
1163                            end
1164                            return
1165                       end
1166
1167                       local msg = m:JSON({ command = command,
1168                                            appid   = win.appid,
1169                                            pid     = win.pid,
1170                                            arg     = arg
1171                       })
1172                       if verbose > 0 then
1173                           print("### <== sending " ..
1174                                 command_name(msg.command) ..
1175                                 " window message to '" .. win.name .. "'")
1176                           if verbose > 1 then
1177                               print(msg)
1178                           end
1179                       end
1180                       sc:send_message(homescreen, msg)
1181
1182                       if oper == 1 then -- create
1183                           local i = input_layer[win.layertype]
1184                           local p = self:application(win.appid)
1185                           local s = p.privileges.screen
1186
1187                           if s == "system" then
1188                               local a = animation({})
1189                               local r = m:JSON({surface = win.surface,
1190                                                 visible = 1,
1191                                                 raise   = 1})
1192                               self:window_request(r,a,0)
1193                           else
1194                               if i then
1195                                   if verbose > 0 then
1196                                       print("do not make resource for " ..
1197                                             "input window")
1198                                   end
1199                               else
1200                                   resclnt:resource_set_create("screen",
1201                                                                "driver",
1202                                                                win.appid,
1203                                                                win.surface)
1204                                   special_screen_sets[win.surface] = true
1205                               end
1206                           end
1207                       elseif oper == 2 then -- destroy
1208                           resclnt:resource_set_destroy("screen", win.surface)
1209                           special_screen_sets[win.surface] = nil
1210                       elseif oper == 6 then -- active
1211                           if win.active then
1212                               local i = input_layer[win.layertype]
1213                               local p = self:application(win.appid)
1214                               local s = p.privileges.screen
1215                               local surface = win.surface
1216                               if not i and s ~= "system" then
1217                                  resclnt:resource_set_acquire("screen",surface)
1218                                  resmgr:window_raise(win.appid, surface, 1)
1219                               end
1220                           end
1221                       end
1222                   end,
1223
1224   layer_update = function(self, oper, layer, mask)
1225                       if verbose > 0 then
1226                           print("### LAYER UPDATE:" ..
1227                                 layer_operation_name(oper) ..
1228                                 " mask: " .. tostring(mask))
1229                           if verbose > 1 then
1230                               print(layer)
1231                           end
1232                       end
1233                       if oper == 3 then -- visible
1234                          local command = 0x10022
1235                          local msg = m:JSON({
1236                                          command = command,
1237                                          appid = "",
1238                                          arg = m:JSON({layer = layer.id,
1239                                                        visible = layer.visible
1240                                          })
1241                                 })
1242                          if verbose > 0 then
1243                             print("### <== sending "..command_name(command)..
1244                                   " layer message")
1245                             if verbose > 1 then
1246                                 print(msg)
1247                             end
1248                          end
1249                          sc:send_message(homescreen, msg)
1250                       else
1251                            if verbose > 0 then
1252                                print("### nothing to do")
1253                            end
1254                       end
1255                  end,
1256
1257   output_update = function(self, oper, out, mask)
1258                       local idx = out.index
1259                       local defidx = self.output_order[idx+1]
1260                       if verbose > 0 then
1261                           print("### OUTPUT UPDATE:" .. oper ..
1262                                 " mask: "..tostring(mask))
1263                       end
1264                       if not defidx then
1265                           return
1266                       end
1267                       print(out)
1268                       local outdef = self.outputs[defidx+1]
1269                       if (oper == 1) then -- create
1270                           if outdef then
1271                               self:output_request(m:JSON({index = idx,
1272                                                           id    = outdef.id,
1273                                                           name  = outdef.name
1274                                                           }))
1275                           end
1276                       elseif (oper == 5) then -- done
1277                           local ads = outdef.areas
1278                           local on = outdef.name
1279                           if ads then
1280                               for name,ad in pairs(ads) do
1281                                   local can = wmgr:canonical_name(on.."."..name)
1282                                   local a = m:JSON({name   = name,
1283                                                     output = out.index})
1284                                   for fld,val in pairs(ad) do
1285                                       a[fld] = self:geometry(out.width,
1286                                                              out.height,
1287                                                              val)
1288                                    end
1289                                    self:area_create(a)
1290                                    resmgr:area_create(area[can], outdef.zone)
1291                               end
1292                           end
1293                       end
1294                   end
1295 }
1296
1297
1298 imgr = input_manager {
1299   inputs = {{ name = "G27 Racing Wheel",
1300               id = 0,
1301               switch = { [2] = {appid="org.tizen.ico.app-soundsample"      },
1302                          [3] = {appid="org.tizen.ico.homescreen", keycode=1},
1303                          [4] = {appid="org.tizen.ico.app-soundsample"      },
1304                          [5] = {appid="org.tizen.ico.homescreen", keycode=2}
1305              }}
1306   },
1307
1308   manager_update = function(self, oper)
1309                        if verbose > 0 then
1310                            print("### <== INPUT MANAGER UPDATE:" ..
1311                                  input_manager_operation_name(oper))
1312                        end
1313                    end,
1314
1315   input_update = function(self, oper, inp, mask)
1316                      if verbose > 0 then
1317                          print("### INPUT UPDATE:" ..
1318                                 input_operation_name(oper) ..
1319                                 " mask: " .. tostring(mask))
1320                           if verbose > 1 then
1321                               print(inp)
1322                           end
1323                       end
1324                  end,
1325   code_update  = function(self, oper, code, mask)
1326                      if verbose > 0 then
1327                          print("### CODE UPDATE: mask: " .. tostring(mask))
1328                          if verbose > 1 then
1329                              print(code)
1330                          end
1331                      end
1332                      local msg = m:JSON({ command = 1,
1333                                           appid = "org.tizen.ico.homescreen",
1334                                           arg = m:JSON({ device = code.device,
1335                                                          time = code.time,
1336                                                          input = code.input,
1337                                                          code = code.id,
1338                                                          state = code.state
1339                                            })
1340                      })
1341                      if verbose > 0 then
1342                          print("### <== sending " ..
1343                                command_name(msg.command) ..
1344                                " input message")
1345                          if verbose > 1 then
1346                              print(msg)
1347                          end
1348                      end
1349                      sc:send_message(homescreen, msg)
1350                  end
1351 }
1352
1353 sc = m:get_system_controller()
1354
1355 -- resource sets
1356 sets = {}
1357
1358 -- special screen resource sets
1359 -- TODO: just rewrite screen resource handling to use regular resource API
1360
1361 special_screen_sets = {}
1362
1363 -- user manager
1364 um = m:UserManager()
1365
1366 connected = false
1367 homescreen = ""
1368 onscreen = ""
1369
1370 -- these shoud be before wmgr:connect() is called
1371 if verbose > 0 then
1372    print("====== creating applications ======")
1373 end
1374 application {
1375     appid          = "default",
1376     area           = "Center.Full",
1377     privileges     = { screen = "none", audio = "none" },
1378     resource_class = "player",
1379     screen_priority = 0
1380 }
1381
1382 application {
1383     appid           = "weston",
1384     area            = "Center.Full",
1385     privileges      = { screen = "system", audio = "none" },
1386     resource_class  = "implicit",
1387     screen_priority = 30
1388 }
1389
1390 application {
1391     appid           = "org.tizen.ico.homescreen",
1392     area            = "Center.Full",
1393     windows         = { {'ico_hs_controlbarwindow', 'Center.Control'} },
1394     privileges      = { screen = "system", audio = "system" },
1395     resource_class  = "player",
1396     screen_priority = 20
1397 }
1398
1399 application {
1400     appid           = "org.tizen.ico.statusbar",
1401     area            = "Center.Status",
1402     privileges      = { screen = "system", audio = "none" },
1403     resource_class  = "player",
1404     screen_priority = 20
1405 }
1406
1407 application {
1408     appid           = "org.tizen.ico.onscreen",
1409     area            = "Center.Full",
1410     privileges      = { screen = "system", audio = "system" },
1411     resource_class  = "player",
1412     screen_priority = 20
1413 }
1414
1415 application {
1416     appid           = "org.tizen.ico.login",
1417     area            = "Center.Full",
1418     privileges      = { screen = "system", audio = "system" },
1419     resource_class  = "player",
1420     screen_priority = 20
1421 }
1422
1423 application {
1424     appid           = "org.tizen.ico.camera_left",
1425     area            = "Center.SysApp.Left",
1426     privileges      = { screen = "system", audio = "none" },
1427     requisites      = { screen = "blinker_left", audio = "none" },
1428     resource_class  = "player",
1429     screen_priority = 30
1430 }
1431
1432 application {
1433     appid           = "org.tizen.ico.camera_right",
1434     area            = "Center.SysApp.Right",
1435     privileges      = { screen = "system", audio = "none" },
1436     requisites      = { screen = "blinker_right", audio = "none" },
1437     resource_class  = "player",
1438     screen_priority = 30
1439 }
1440
1441 application {
1442     appid           = "net.zmap.navi",
1443     area            = "Center.Full",
1444     privileges      = { screen = "none", audio = "none" },
1445     resource_class  = "navigator",
1446     screen_priority = 30
1447 }
1448
1449 application {
1450     appid           = "GV3ySIINq7.GhostCluster",
1451     area            = "Center.Full",
1452     privileges      = { screen = "none", audio = "none" },
1453     resource_class  = "system",
1454     screen_priority = 30
1455 }
1456
1457 if sc then
1458     sc.client_handler = function (self, cid, msg)
1459         local command = msg.command
1460         local appid = msg.appid
1461         if verbose > 0 then
1462             print('### ==> client handler:')
1463             if verbose > 1 then
1464                 print(msg)
1465             end
1466         end
1467
1468         -- known commands: 1 for SEND_APPID, synthetic command 0xFFFF for
1469         -- disconnection
1470
1471         if command == 0xFFFF then
1472             if verbose > 1 then
1473                 print('client ' .. cid .. ' disconnected')
1474             end
1475             if msg.appid == homescreen then
1476                 homescreen = ""
1477                 for i,v in pairs(special_screen_sets) do
1478                     resclnt:resource_set_destroy("screen", i)
1479                     special_screen_sets[i] = nil
1480                 end
1481             end
1482             return
1483         end
1484
1485         -- handle the connection to weston
1486
1487         if appid then
1488             if appid == "org.tizen.ico.homescreen" then
1489                 print('Setting homescreen='..appid)
1490                 homescreen = appid
1491                 if command and command == 1 then
1492                     send_driving_mode_to_home_screen()
1493                     send_night_mode_to_home_screen()
1494                 end
1495             elseif appid == "org.tizen.ico.onscreen" then
1496                 onscreen = appid
1497             end
1498
1499             if not connected and appid == "org.tizen.ico.homescreen" then
1500                 print('Trying to connect to weston...')
1501                 connected = wmgr:connect()
1502             end
1503         end
1504     end
1505
1506     sc.generic_handler = function (self, cid, msg)
1507         if verbose > 0 then
1508             print('### ==> generic handler:')
1509             if verbose > 1 then
1510                print(msg)
1511             end
1512         end
1513     end
1514
1515     sc.window_handler = function (self, cid, msg)
1516         if verbose > 0 then
1517             print('### ==> received ' ..
1518                    command_name(msg.command) .. ' message')
1519             if verbose > 1 then
1520                 print(tostring(msg))
1521             end
1522         end
1523
1524         local a = animation({})
1525         local nores = false
1526         if msg.command == 0x10003 then       -- ico SHOW command
1527             local raise_mask = 0x01000000
1528             local lower_mask = 0x02000000
1529             local nores_mask = 0x40000000
1530             local time_mask  = 0x00ffffff
1531             msg.arg.visible = 1
1532             if msg.arg then
1533                 local time = 200
1534                 if  msg.arg.anim_time then
1535                     local t = msg.arg.anim_time
1536                     time = m:AND(t, time_mask)
1537                     nores = not m:AND(t, nores_mask)
1538                     if m:AND(t, raise_mask) then
1539                         msg.arg.raise = 1
1540                     elseif m:AND(t, lower_mask) then
1541                         msg.arg.raise = 0
1542                     end
1543                 end
1544                 if msg.arg.anim_name then
1545                     a.show = { msg.arg.anim_name, time }
1546                     print('time: ' .. tostring(a.show[2]))
1547                 end
1548             end
1549             if not nores then
1550                 local p = wmgr:application(msg.appid)
1551                 local s = p.privileges.screen
1552                 if s == "system" then
1553                     nores = true
1554                     if not msg.arg.raise then
1555                         msg.arg.raise = 1
1556                     end
1557                 end
1558             end
1559             if verbose > 2 then
1560                 print('### ==> SHOW')
1561                 print(tostring(msg.arg))
1562             end
1563             if nores then
1564                 wmgr:window_request(msg.arg, a, 0)
1565             else
1566                 local surface = msg.arg.surface
1567                 resclnt:resource_set_acquire("screen", surface)
1568                 resmgr:window_raise(msg.appid, surface, 1)
1569             end
1570         elseif msg.command == 0x10004 then   -- ico HIDE command
1571             local raise_mask = 0x01000000
1572             local lower_mask = 0x02000000
1573             local nores_mask = 0x40000000
1574             local time_mask  = 0x00ffffff
1575             msg.arg.visible = 0
1576             if msg.arg then
1577                 local time = 200
1578                 if msg.arg.anim_time then
1579                     local t = msg.arg.anim_time
1580                     time = m:AND(t, time_mask)
1581                     nores = not m:AND(t, nores_mask)
1582                 end
1583                 if msg.arg.anim_name then
1584                     a.hide = { msg.arg.anim_name, time }
1585                     print('hide animation time: ' .. tostring(a.hide[2]))
1586                 end
1587             end
1588             if not nores then
1589                 local p = wmgr:application(msg.appid)
1590                 local s = p.privileges.screen
1591                 if s == "system" then
1592                     nores = true
1593                     msg.arg.raise = 0
1594                 end
1595             end
1596             if verbose > 2 then
1597                 print('### ==> HIDE REQUEST')
1598                 print(tostring(msg.arg))
1599             end
1600             if nores then
1601                 wmgr:window_request(msg.arg, a, 0)
1602             else
1603                 resmgr:window_raise(msg.appid, msg.arg.surface, -1)
1604             end
1605         elseif msg.command == 0x10005 then   -- ico MOVE
1606             if verbose > 2 then
1607                 print('### ==> MOVE REQUEST')
1608                 print(tostring(msg.arg))
1609             end
1610             if msg.arg.zone then
1611                 msg.arg.area = msg.arg.zone
1612             end
1613             wmgr:window_request(msg.arg, a, 0)
1614             -- TODO: handle if area changed
1615         elseif msg.command == 0x10006 then   -- ico ACTIVE
1616             if not msg.arg.active then
1617                 msg.arg.active = 3 -- pointer + keyboard
1618             end
1619             if verbose > 2 then
1620                 print('### ==> ACTIVE REQUEST')
1621                 print(tostring(msg.arg))
1622             end
1623             wmgr:window_request(msg.arg, a, 0)
1624         elseif msg.command == 0x10007 then   -- ico CHANGE_LAYER
1625             if verbose > 2 then
1626                 print('### ==> CHANGE_LAYER REQUEST')
1627                 print(tostring(msg.arg))
1628             end
1629             --[[
1630             if msg.arg.layer ~= 4 or msg.arg.layer ~= 5 then
1631                 print("do not change layer for other than cursor or touch")
1632                 return
1633             end
1634             --]]
1635             wmgr:window_request(msg.arg, a, 0)
1636         elseif msg.command == 0x10011 then   -- ico MAP_THUMB
1637             local framerate = msg.arg.framerate
1638             if not framerate or framerate < 0 then
1639                 framerate = 0
1640             end
1641             msg.arg.mapped = 1
1642             if verbose > 2 then
1643                 print('### ==> MAP_THUMB REQUEST')
1644                 print(msg.arg)
1645                 print('framerate: '..framerate)
1646             end
1647             wmgr:window_request(msg.arg, a, framerate)
1648         elseif msg.command == 0x10012 then   -- ico UNMAP_THUMB
1649             msg.arg.mapped = 0
1650             if verbose > 2 then
1651                 print('### ==> UNMAP_THUMB REQUEST')
1652                 print(msg.arg)
1653             end
1654             wmgr:window_request(msg.arg, a, 0)
1655         elseif msg.command == 0x10013 then -- ico MAP_BUFFER command
1656             local shmname = msg.arg.anim_name
1657             local bufsize = msg.arg.width
1658             local bufnum  = msg.arg.height
1659             if shmname and bufsize and bufnum then
1660                 if verbose > 2 then
1661                     print('### ==> MAP_BUFFER REQUEST')
1662                     print("shmaname='" .. shmname ..
1663                           "' bufsize='" .. bufsize ..
1664                           " bufnum=" .. bufnum)
1665                 end
1666                 wmgr:buffer_request(shmname, bufsize, bufnum)
1667             end
1668         elseif msg.command == 0x10020 then   -- ico SHOW_LAYER command
1669             msg.arg.visible = 1
1670             if verbose > 2 then
1671                 print('### ==> SHOW_LAYER REQUEST')
1672                 print(msg.arg)
1673             end
1674             wmgr:layer_request(msg.arg)
1675         elseif msg.command == 0x10021 then   -- ico HIDE_LAYER command
1676             msg.arg.visible = 0
1677             if verbose > 2 then
1678                 print('### ==> HIDE_LAYER REQUEST')
1679                 print(msg.arg)
1680             end
1681             wmgr:layer_request(msg.arg)
1682         end
1683     end
1684
1685     sc.input_handler = function (self, cid, msg)
1686         if verbose > 0 then
1687             print('### ==> input handler: ' .. command_name(msg.command))
1688             if verbose > 1 then
1689                 print(msg)
1690             end
1691         end
1692         if msg.command == 0x20001 then -- add_input
1693             msg.arg.appid = msg.appid
1694             if verbose > 2 then
1695                 print('### ==> ADD_INPUT REQUEST')
1696                 print(tostring(msg.arg))
1697             end
1698             imgr:input_request(msg.arg)
1699         elseif msg.command == 0x20002 then -- del_input
1700             msg.arg.appid = ''
1701             if verbose > 2 then
1702                 print('### ==> DEL_INPUT REQUEST')
1703                 print(tostring(msg.arg))
1704             end
1705             imgr:input_request(msg.arg)
1706         elseif msg.command == 0x20003 then -- send_input
1707         end
1708     end
1709
1710     sc.user_handler = function (self, cid, msg)
1711         if verbose > 0 then
1712             print('### ==> user handler: ' .. command_name(msg.command))
1713             if verbose > 1 then
1714                 print(msg)
1715             end
1716         end
1717
1718         if not um then
1719             print("User Manager not initialized")
1720             return
1721         end
1722
1723         if msg.command == 0x00030001 then -- MSG_CMD_CHANGE_USER
1724             print("command CHANGE_USER")
1725             if not msg.arg then
1726                 print("invalid message")
1727                 return
1728             end
1729
1730             username = msg.arg.user
1731             passwd = msg.arg.pass
1732
1733             if not username then
1734                 username = ""
1735             end
1736
1737             if not passwd then
1738                 passwd = ""
1739             end
1740
1741             success = um:changeUser(username, passwd)
1742
1743             if not success then
1744                 reply = m.JSON({
1745                     appid = msg.appid,
1746                     command = cmd
1747                 })
1748                 if sc:send_message(msg.appid, reply) then
1749                     print('*** sent authentication failed message')
1750                 else
1751                     print('*** failed to send authentication failed message')
1752                 end
1753             end
1754
1755         elseif msg.command == 0x00030002 then -- MSG_CMD_GET_USERLIST
1756             print("command GET_USERLIST")
1757             if not msg.appid then
1758                 print("invalid message")
1759                 return
1760             end
1761
1762             users, currentUser = um:getUserList()
1763
1764             if not users then
1765                 print("failed to get user list")
1766                 return
1767             end
1768
1769             nUsers = 0
1770
1771             for i,v in pairs(users) do
1772                 nUsers = nUsers + 1
1773             end
1774
1775             if not currentUser then
1776                 currentUser = ""
1777             end
1778
1779             if verbose > 1 then
1780                 print("current user: " .. currentUser)
1781                 print("user list:")
1782                 for i,v in pairs(users) do
1783                     print(v)
1784                 end
1785             end
1786
1787             reply = m.JSON({
1788                 appid = msg.appid,
1789                 command = cmd,
1790                 arg = m.JSON({
1791                     user_num = nUsers,
1792                     user_list = users,
1793                     user_login = currentUser
1794                 })
1795             })
1796
1797             if verbose > 1 then
1798                 print("### <== GetUserList reply: " .. tostring(reply))
1799             end
1800
1801             if sc:send_message(msg.appid, reply) then
1802                 print('*** reply OK')
1803             else
1804                 print('*** reply FAILED')
1805             end
1806
1807         elseif msg.command == 0x00030003 then -- MSG_CMD_GET_LASTINFO
1808             print("command GET_LASTINFO")
1809             if not msg.appid then
1810                 print("invalid message")
1811                 return
1812             end
1813
1814             lastInfo = um:getLastInfo(msg.appid)
1815
1816             if not lastInfo then
1817                 print("failed to get last info for app" .. msg.appid)
1818                 return
1819             end
1820
1821             reply = m.JSON({
1822                 appid = msg.appid,
1823                 command = cmd,
1824                 arg = m.JSON({
1825                     lastinfo = lastinfo
1826                 })
1827             })
1828
1829             if sc:send_message(msg.appid, reply) then
1830                 print('*** reply OK')
1831             else
1832                 print('*** reply FAILED')
1833             end
1834
1835         elseif msg.command == 0x00030004 then -- MSG_CMD_SET_LASTINFO
1836             print("command SET_LASTINFO")
1837             if not msg.arg or not msg.appid then
1838                 print("invalid message")
1839                 return
1840             end
1841
1842             lastInfo = um:setLastInfo(msg.appid, msg.arg.lastinfo)
1843         end
1844     end
1845
1846     sc.resource_handler = function (self, cid, msg)
1847         if verbose > 0 then
1848             print('### ==> resource handler: ' .. command_name(msg.command))
1849             if verbose > 1 then
1850                 print(msg)
1851             end
1852         end
1853
1854         createResourceSet = function (ctl, client, msg)
1855             cb = function(rset, data)
1856                 print("> resource callback")
1857
1858                 -- type is either basic (0) or interrupt (1)
1859                 requestType = 0
1860                 if msg.res.type then
1861                     requestType = msg.res.type
1862                 end
1863
1864                 if rset.acquired then
1865                     cmd = 0x00040001 -- acquire
1866                 else
1867                     cmd = 0x00040002 -- release
1868                 end
1869
1870                 reply = m.JSON({
1871                         appid = data.client,
1872                         command = cmd,
1873                         res = {
1874                             type = requestType
1875                         }
1876                     })
1877
1878                 if rset.resources.audio_playback then
1879                     reply.res.sound = {
1880                         zone = "driver",
1881                         name = msg.appid,
1882                         adjust = 0,
1883                         -- id = "0"
1884                     }
1885                 end
1886
1887                 if rset.resources.display then
1888                     reply.res.window = {
1889                         zone = "driver",
1890                         name = msg.appid,
1891                         -- id = "0"
1892                     }
1893                 end
1894
1895                 if rset.resources.input then
1896                     reply.res.input = {
1897                         name = msg.appid,
1898                         event = 0
1899                     }
1900                 end
1901                 print("sending message to client: " .. data.client)
1902
1903                 if sc:send_message(data.client, reply) then
1904                     print('*** reply OK')
1905                 else
1906                     print('*** reply FAILED')
1907                 end
1908             end
1909
1910             rset = m:ResourceSet({
1911                     application_class = "player",
1912                     zone = "driver", -- msg.zone ("full")
1913                     callback = cb
1914                 })
1915
1916             rset.data = {
1917                 cid = cid,
1918                 ctl = ctl
1919             }
1920
1921             if msg.res.sound then
1922                 rset:addResource({
1923                         resource_name = "audio_playback"
1924                     })
1925                 rset.resources.audio_playback.attributes.pid = tostring(msg.pid)
1926                 rset.resources.audio_playback.attributes.appid = msg.appid
1927                 print("sound name: " .. msg.res.sound.name)
1928                 print("sound zone:" .. msg.res.sound.zone)
1929                 print("sound adjust: " .. tostring(msg.res.sound.adjust))
1930                 if msg.res.sound.id then
1931                     print("sound id: " .. msg.res.sound.id)
1932                 end
1933             end
1934
1935             if msg.res.input then
1936                 rset:addResource({
1937                         resource_name = "input"
1938                     })
1939                 rset.resources.input.attributes.pid = tostring(msg.pid)
1940                 rset.resources.input.attributes.appid = msg.appid
1941                 print("input name: " .. msg.res.sound.name)
1942                 print("input event:" .. tostring(msg.res.input.event))
1943             end
1944
1945             if msg.res.window then
1946                 rset:addResource({
1947                         resource_name = "display"
1948                     })
1949                 rset.resources.display.attributes.pid = tostring(msg.pid)
1950                 rset.resources.display.attributes.appid = msg.appid
1951                 print("display name: " .. msg.res.display.name)
1952                 print("display zone:" .. msg.res.display.zone)
1953                 if msg.res.display.id then
1954                     print("display id: " .. msg.res.display.id)
1955                 end
1956             end
1957
1958             return rset
1959         end
1960
1961         -- parse the message
1962
1963         -- fields common to all messages:
1964         --      msg.command
1965         --      msg.appid
1966         --      msg.pid
1967
1968         if msg.command == 0x00040011 then -- MSG_CMD_CREATE_RES
1969             print("command CREATE")
1970
1971             if not sets.cid then
1972                 sets.cid = createResourceSet(self, cid, msg)
1973             end
1974
1975         elseif msg.command == 0x00040012 then -- MSG_CMD_DESTORY_RES
1976             print("command DESTROY")
1977
1978             if sets.cid then
1979                 sets.cid:release()
1980             end
1981
1982             sets.cid = nil -- garbage collecting
1983
1984         elseif msg.command == 0x00040001 then -- MSG_CMD_ACQUIRE_RES
1985             print("command ACQUIRE")
1986
1987             if not sets.cid then
1988                 sets.cid = createResourceSet(self, cid, msg)
1989             end
1990
1991             sets.cid:acquire()
1992
1993         elseif msg.command == 0x00040002 then -- MSG_CMD_RELEASE_RES
1994             print("command RELEASE")
1995
1996             if sets.cid then
1997                 sets.cid:release()
1998             end
1999
2000         elseif msg.command == 0x00040003 then -- MSG_CMD_DEPRIVE_RES
2001             print("command DEPRIVE")
2002
2003         elseif msg.command == 0x00040004 then -- MSG_CMD_WAITING_RES
2004             print("command WAITING")
2005
2006         elseif msg.command == 0x00040005 then -- MSG_CMD_REVERT_RES
2007             print("command REVERT")
2008         end
2009     end
2010
2011     sc.inputdev_handler = function (self, cid, msg)
2012         if verbose > 0 then
2013             print('*** inputdev handler: ' .. command_name(msg.command))
2014             if verbose > 1 then
2015                 print(msg)
2016             end
2017         end
2018     end
2019 end
2020
2021 function send_driving_mode_to_home_screen()
2022     if homescreen == "" then
2023         return
2024     end
2025
2026     local driving_mode = mdb.select.select_driving_mode.single_value
2027
2028     if not driving_mode then driving_mode = 0 end
2029
2030     local reply = m:JSON({ command = 0x60001,
2031                            arg     = m:JSON({ stateid = 1,
2032                                               state   = driving_mode
2033                                      })
2034                   })
2035
2036     if verbose > 0 then
2037         print("### <== sending " .. command_name(reply.command) .. " message")
2038         if verbose > 1 then
2039             print(reply)
2040         end
2041     end
2042
2043     sc:send_message(homescreen, reply)
2044 end
2045
2046 function send_night_mode_to_home_screen()
2047     if homescreen == "" then
2048         return
2049     end
2050
2051     local night_mode = mdb.select.select_night_mode.single_value
2052
2053     if not night_mode then night_mode = 0 end
2054
2055     local reply = m:JSON({ command = 0x60001,
2056                            arg     = m:JSON({ stateid = 2,
2057                                               state   = night_mode
2058                                      })
2059                   })
2060
2061     if verbose > 0 then
2062         print("### <== sending " .. command_name(reply.command) .. " message")
2063         if verbose > 1 then
2064             print(reply)
2065         end
2066      end
2067
2068      sc:send_message(homescreen, reply)
2069 end
2070
2071 -- we should have 'audio_playback' defined by now
2072 m:try_load_plugin('telephony')